package April.Twenty_six;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/*
修改，
利用PrepareStatement接口，实现对数据库的更新操作
 */
public class JdbcDemo_3 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.jdbc.Driver");
//        2. 获取连接对象
        String url = "jdbc:mysql://127.0.0.1:3306/myBase?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC";
        String userName = "root";
        String password = "123";
        Connection con = DriverManager.getConnection(url, userName, password);

//        拼写sql语句，参数采用？占位

        String sql = "UPDATE product SET pname=?,price=? WHERE id=?;";
//        调用数据库连接对象的con的方法prepareStatement获取sql语句的预编译对象
        PreparedStatement pst = con.prepareStatement(sql);
//        调用pst的方法setXXX设置？占位
        pst.setObject(1,"老人机221");
        pst.setObject(2,100);
        pst.setObject(3,2);
        pst.executeUpdate();

        pst.close();
        con.close();

    }
}
